﻿package com.tvein.sanguo.swc.action.tool
{
	//flash
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.text.TextFormat;
	import fl.controls.dataGridClasses.*;
	import fl.controls.DataGrid;
	import fl.data.DataProvider;
	import fl.events.ListEvent;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.OverLay;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class ToolAdvice extends MovieClip
	{
		public static const NAME:String = "Tool-Advice";

		private var effectVol:int = 100;
		private var overLay:OverLay;
		
		private var advices:Object;
		private var adviceList:DataGrid;
		
		public function ToolAdvice()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			overLay = new OverLay();			
			this.addChild(overLay);
			this.setChildIndex(overLay, 0);
			
			adviceList = new DataGrid();
			adviceList.x = 5;
			adviceList.y = 70;
			adviceList.width = 590;
			adviceList.height = 310;
			this.addChild(adviceList);
			
			var style:TextFormat = new TextFormat();
			style.color = 0xff6600;
			style.size = 16;
			style.font = "LiSu";
			style.align = "center";
			
			adviceList.setRendererStyle("textFormat", style);
			
			var c0:DataGridColumn = new DataGridColumn("TYPE");
			var c1:DataGridColumn = new DataGridColumn("信息");
				
			c0.visible = false;
			c1.width = 590;
			
			adviceList.addColumn(c0);
			adviceList.addColumn(c1);
			
			btnClose.addEventListener(MouseEvent.CLICK, onClick);
			btnOwn.addEventListener(MouseEvent.CLICK, onClick);
			btnRelation.addEventListener(MouseEvent.CLICK, onClick);
			btnOutside.addEventListener(MouseEvent.CLICK, onClick);
			btnGuild.addEventListener(MouseEvent.CLICK, onClick);
			
			mcFrame.reSize(600, 450);
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setData":
					advices = params;
					setAdviceData("All");
					break;
			}
		}
		
		private function setAdviceData(type:String):void
		{
			var i:int;
			
			adviceList.removeAll();
			adviceList.invalidateList();
			
			switch(type)
			{
				case "All":
					for (i = 0; i < advices.length; i++)
					{
						adviceList.addItem({"TYPE":advices[i][0], "信息":String(advices[i][1])});
					}
					break;
				case "Own":
					for (i = 0; i < advices.length; i++)
					{
						if (advices[i][0] == 1)
						{
							adviceList.addItem({"TYPE":advices[i][0], "信息":String(advices[i][1])});
						}
					}
					break;
				case "Relation":
					for (i = 0; i < advices.length; i++)
					{
						if (advices[i][0] == 2)
						{
							adviceList.addItem({"TYPE":advices[i][0], "信息":String(advices[i][1])});
						}
					}
					break;
				case "Outside":
					for (i = 0; i < advices.length; i++)
					{
						if (advices[i][0] == 3)
						{
							adviceList.addItem({"TYPE":advices[i][0], "信息":String(advices[i][1])});
						}
					}
					break;
				case "Guild":
					for (i = 0; i < advices.length; i++)
					{
						if (advices[i][0] == 4)
						{
							adviceList.addItem({"TYPE":advices[i][0], "信息":String(advices[i][1])});
						}
					}
					break;
			}			
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			btnClose.removeEventListener(MouseEvent.CLICK, onClick);
			btnOwn.removeEventListener(MouseEvent.CLICK, onClick);
			btnRelation.removeEventListener(MouseEvent.CLICK, onClick);
			btnOutside.removeEventListener(MouseEvent.CLICK, onClick);
			btnGuild.removeEventListener(MouseEvent.CLICK, onClick);
		}
		
		private function _resizeUI(evt:Event = null):void
		{
			overLay.x = overLay.y = overLay.width = overLay.height = 0;
			
			this.x = (stage.stageWidth - this.width) * .5
			
			if ((stage.stageHeight - this.height) > 150)
			{
				this.y = (stage.stageHeight - this.height) * .3;
			}
			else
			{
				this.y = 45;
			}
			
			overLay.x = 0 - this.x;
			overLay.y = 0 - this.y;
			overLay.width = stage.stageWidth;
			overLay.height = stage.stageHeight;
		}

		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnClose":
					dispatchEvent(new SwcEvent(NAME, ["-close", "ToolAdvice"]));
					break;
				case "btnOwn":
					setAdviceData("Own");
					break;
				case "btnRelation":
					setAdviceData("Relation");
					break;
				case "btnOutside":
					setAdviceData("Outside");
					break;
				case "btnGuild":
					setAdviceData("Guild");
					break;
			}
		}
	}
}